using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Appwidget;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Moogle
{
    /// <summary>
    /// Pop/Remind Activity Class.  This activity is used to modify the user existing pop reminders.
    /// It's target consumer is the ActivityRemind class.
    /// </summary>
    [Activity(Label = "Pop Reminders", MainLauncher = false, Icon = "@drawable/icon", NoHistory = true, Theme = "@android:style/Theme.Holo.Light")]
    [IntentFilter(new[] { Intent.ActionMain })]
    public class ActivityPopRemind : Activity
    {
        #region Local Memory
        
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string __C_TAG = "ActivityPopRemind";
       
        private Button __M_buttonPickTop = null;
        private Button __M_buttonPickBottom = null;
        private Button __M_buttonMenu = null;
        private Button __M_buttonBack = null;
        private TextView __M_textViewBottomSched = null;
        private TextView __M_textViewTopSched = null;
        private dbCommands __M_clsDbCommands = null;
        private ListView __M_listViewPopTimes = null;
        private List<PopTime> __M_listAllPopTimes = null;
        private String __M_stringMedicationNameTop = string.Empty;
        private String __M_stringMedicationNameBottom = string.Empty;
        private String __M_stringTimeStampsTop = string.Empty;
        private String __M_stringTimeStampsBottom = string.Empty;
        private List<PopTime> __M_listSelectedPopTimesTop = new List<PopTime>();
        private List<PopTime> __M_listSelectedPopTimesBottom = new List<PopTime>();
 
        private Boolean __M_booleanNoDataTop = false;
        private Boolean __M_booleanNoDataBottom = false;

        #endregion
        #region OnCreate Control Function
        /// <summary>
        /// Class (or Activity) entry point.
        /// </summary>
        /// <param name="bundle">Android.OS.Bundle object instance that can be used to pass data to activity at startup.</param>
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            Log.Info(__C_TAG, "OnCreate started.");

            try
            {
                /// Load the activities layout 
                this.SetContentView(Resource.Layout.remindpop);
                this.__M_buttonPickTop = (Button)this.FindViewById(Resource.Id.pickTopTime);
                this.__M_buttonPickBottom = (Button)this.FindViewById(Resource.Id.pickBottomTime);
                this.__M_buttonMenu = (Button)this.FindViewById(Resource.Id.buttonMenu);

                /// add a click listener to the Pick Date button        
                this.__M_buttonPickTop.Click += (sender, e) =>
                {
                    var _topedit = new Intent(this, typeof(ActivityPopRemindEdit));
                    _topedit.PutExtra("Medication", Resources.GetString(Resource.String.remind_pop_top_title_text));
                    StartActivity(_topedit);
                };
                this.__M_buttonPickBottom.Click += (sender, e) =>
                {
                    var _bottomedit = new Intent(this, typeof(ActivityPopRemindEdit));
                    _bottomedit.PutExtra("Medication", Resources.GetString(Resource.String.remind_pop_bottom_title_text));
                    StartActivity(_bottomedit);
                };
                this.__M_buttonMenu.Click += (sender, e) =>
                {
                    var _menu = new Intent(this, typeof(ActivityRemind));
                    StartActivity(_menu);
                };

              
                /// Configure the back button
                #region ...
                this.__M_buttonBack = (Button)this.FindViewById(Resource.Id.doBack);
                this.__M_buttonBack.Click += delegate
                {
                    var _IntentShow = new Intent(this, typeof(ActivityRemind));
                    StartActivity(_IntentShow);

                }; // end delegate
                #endregion

                /// Get All PopTimes from the database
                this.__M_clsDbCommands = new dbCommands(this);
                this.__M_listAllPopTimes = (List<PopTime>)this.__M_clsDbCommands.GetAllPopTimes();

                /// Store the top medication name 
                this.__M_stringMedicationNameTop = Resources.GetString(Resource.String.remind_pop_top_title_text);

                /// Store the bottom medication name 
                this.__M_stringMedicationNameBottom = Resources.GetString(Resource.String.remind_pop_bottom_title_text);

                if (this.__M_listViewPopTimes.Count > 0)
                {
                    /// Populate the selected Medication Pop Time Top/Bottom Lists
                    foreach (PopTime _pop in this.__M_listAllPopTimes)
                    {
                        if (_pop.MedicationName.Trim().ToUpper() == this.__M_stringMedicationNameTop.Trim().ToUpper())
                        {
                            this.__M_listSelectedPopTimesTop.Add(_pop);
                        } // end if

                        if (_pop.MedicationName.Trim().ToUpper() == this.__M_stringMedicationNameBottom.Trim().ToUpper())
                        {
                            this.__M_listSelectedPopTimesBottom.Add(_pop);
                        } // end if
                    } // end foreach

                    if (this.__M_listSelectedPopTimesTop.Count > 0)
                    {

                        /// Build the top time stamp string based on the selected
                        /// pop times collection
                        #region ...
                        int _popIndex = 1;

                        foreach (PopTime _clsSelPop in this.__M_listSelectedPopTimesTop)
                        {
                            String _strHour = _clsSelPop.Hour.ToString();
                            String _strMin = _clsSelPop.Minute.ToString();
                            String _strAmPm = string.Empty;
                            String _strTimeStamp = string.Empty;

                            if (_clsSelPop.AnteMeridiem == true)
                                _strAmPm = " AM";
                            else
                                _strAmPm = " PM";

                            if (_strHour.Length == 1)
                                _strHour = "0" + _strHour;
                            if (_strMin.Length == 1)
                                _strMin = "0" + _strMin;

                            _strTimeStamp = _strHour + ":" + _strMin + _strAmPm;

                            this.__M_stringTimeStampsTop += _strTimeStamp;

                            if (_popIndex < this.__M_listSelectedPopTimesTop.Count)
                                this.__M_stringTimeStampsTop += ", ";
                        } // end foreach
                        #endregion
                    } // end if
                    else
                        this.__M_booleanNoDataTop = true;

                    if (this.__M_listSelectedPopTimesBottom.Count > 0)
                    {

                        /// Build the top time stamp string based on the selected
                        /// pop times collection
                        #region ...
                        int _popIndex = 1;

                        foreach (PopTime _clsSelPop in this.__M_listSelectedPopTimesBottom)
                        {
                            String _strHour = _clsSelPop.Hour.ToString();
                            String _strMin = _clsSelPop.Minute.ToString();
                            String _strAmPm = string.Empty;
                            String _strTimeStamp = string.Empty;

                            if (_clsSelPop.AnteMeridiem == true)
                                _strAmPm = " AM";
                            else
                                _strAmPm = " PM";

                            if (_strHour.Length == 1)
                                _strHour = "0" + _strHour;
                            if (_strMin.Length == 1)
                                _strMin = "0" + _strMin;

                            _strTimeStamp = _strHour + ":" + _strMin + _strAmPm;

                            this.__M_stringTimeStampsBottom += _strTimeStamp;

                            if (_popIndex < this.__M_listSelectedPopTimesBottom.Count)
                                this.__M_stringTimeStampsBottom += ", ";
                        } // end foreach
                        #endregion
                    } // end if
                    else
                        this.__M_booleanNoDataBottom = true;

                } // end if
                else
                {
                    this.__M_booleanNoDataTop = true;
                    this.__M_booleanNoDataBottom = true;
                } // else


                /// If the top no data flag is false, then
                /// disable the no data message.
                if (!this.__M_booleanNoDataTop)
                    this.__M_stringTimeStampsTop = "N/A";

                /// If the top no data flag is false, then
                /// disable the no data message.
                if (!this.__M_booleanNoDataBottom)
                    this.__M_stringTimeStampsBottom = "N/A";

                /// Configure the Top TextView Control
                #region ...
                this.__M_textViewTopSched = FindViewById<TextView>(Resource.Id.textViewPopTopSched);
                this.__M_textViewTopSched.Text = this.__M_stringTimeStampsTop;
                #endregion

                /// Configure the Top TextView Control
                #region ...
                this.__M_textViewBottomSched = FindViewById<TextView>(Resource.Id.textViewPopBottomSched);
                this.__M_textViewBottomSched.Text = this.__M_stringTimeStampsBottom;
                #endregion

            } // end try
            catch (SystemException err)
            {
                Log.Error(__C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(__C_TAG, "OnCreate ended.");
            } // end finally
        }
        #endregion
       
    }
}